package com.hex.liyue.commonmodule.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.presenter.IWebView;
import com.hex.liyue.commonmodule.utils.StatusBarCompatUtils;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by yukuo on 2016/4/26.
 * 这是一个网页页面
 */
public class WebActivity extends BaseActivity implements IWebView, View.OnClickListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.tv_toobar_right)
    TextView tvToobarRight;
    @Bind(R.id.pb_web)
    ProgressBar pbWeb;
    @Bind(R.id.wb_web)
    WebView wbWeb;
    private String url;


    @Override
    protected void bindPresenter() {

    }

    @Override
    protected void initData(Bundle extras) {
        StatusBarCompatUtils.compat(this);
        tvToobarTitle.setText(extras.getString("title"));
        url = extras.getString("url");
        wbWeb.setVerticalScrollbarOverlay(true); //指定的垂直滚动条有叠加样式
        WebSettings settings = wbWeb.getSettings();
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放
    }

    @Override
    protected void onStart() {
        super.onStart();
        /**
         * 防止使用默认的浏览器
         */
        wbWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        wbWeb.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                pbWeb.setProgress(newProgress);
                if (newProgress == 100) {
                    pbWeb.setVisibility(View.INVISIBLE);
                } else {
                    pbWeb.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, newProgress);
            }

        });
        wbWeb.loadUrl(url);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_web;
    }

    @Override
    protected void initToolbar() {
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarRight.setVisibility(View.GONE);
    }

    @Override
    @OnClick({R.id.iv_toobar_back})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_toobar_back:
                onBackPressed();
                break;
        }
    }
}
